package simel;

import java.util.*;
import java.io.*;

public class LandonAgent implements Agent
{
    Simulator sim;
    int directions[];
    boolean initialMoveFinished[];
    int floorFreqs[];

    /** Note: Agents must have an empty constructor! **/
    public LandonAgent()
    {
    }

    /** init() will be called before any other method **/
    public void init(Simulator sim)
    {
        this.sim = sim;

        directions = new int[sim.getElevators().size()];
        initialMoveFinished = new boolean[sim.getElevators().size()];
        floorFreqs = new int[sim.floorMax];

        for(int i=0; i < sim.floorMax; i++){
			floorFreqs[i] = 0;
		}

        Random r = new Random(0);

        // send each elevator to a random floor to start with.
        for (int i = 0; i < sim.getElevators().size(); i++) {
            Elevator e = sim.getElevators().get(i);
            e.setDestination(e.getFloorMin() + r.nextInt(e.getFloorMax() - e.getFloorMin()));
        }
    }

    // just send the elevators up and down and up and down.
    public void update(Simulator sim, ArrayList<Passenger> passengers, ArrayList<Elevator> elevators)
    {

		for (int j = 0; j < passengers.size(); j++) {
				Passenger p = passengers.get(j);
				if(p.elevator == -1) // waiting
					floorFreqs[p.floor-1]++;
		}

        for (int i = 0; i < elevators.size(); i++) {
            Elevator e = elevators.get(i);

            // If the elevator still hasn't arrived at its initial starting place, just wait.
            if (!initialMoveFinished[i]) {
                if (e.getFloor() == e.getDestination()) {
                    initialMoveFinished[i] = true;
                    // we've arrived. fall through to usual logic below...
                } else {
                    // keep waiting
                    continue;
                }
            }




            // Landon code

			boolean emptyElevator = true;
			int farthestPass = e.getFloor();
            for (int j = 0; j < passengers.size(); j++) {
				Passenger p = passengers.get(j);
				if(p.elevator == i){ // this elevator
					// leave direction the same, do nothing else
					emptyElevator = false;
					j = passengers.size();
				}

				if(p.elevator == -1){
					if(directions[i] == 1){
						if(p.floor > farthestPass)
							farthestPass = p.floor;
					} else if(directions[i] == -1){
						if(p.floor < farthestPass)
							farthestPass = p.floor;
					}
				}
			}

			if(emptyElevator){
				// if there's nobody farther along your path then reverse direction
				//if(farthestPass == e.getFloor())
				//	directions[i] = -1*directions[i];
				// or, if there's an elevator on your floor in the same direction, then reverse direction
				//for (int j = i+1; j < elevators.size(); j++) {
            	//		Elevator c = elevators.get(j);
            			// check to see if elevator is on the same "track"
            	//		if(c.getFloorMin() == e.getFloorMin() && c.getFloorMax() == e.getFloorMax()){
            	//			if(Math.abs(c.getFloor() - e.getFloor()) < 1 && directions[i] == directions[j]){
								//directions[i] = !directions[i];
								directions[i] = 0;
				//				j = elevators.size();
				//			}
				//		}
				//}

			}

			// dumb code
			//if (e.getFloor() == e.getFloorMin())
            //    directions[i] = 1;

            //if (e.getFloor() == e.getFloorMax())
            //    directions[i] = -1;

            if (directions[i] == 1)
                e.setDestination(e.getFloor() + 1);
            else if(directions[i] == -1)
                e.setDestination(e.getFloor() - 1);
            else {
            	int maxFreq = 0;
            	int maxPlace = e.getFloorMax();
            	for(int k=e.getFloorMin(); k < e.getFloorMax(); k++){
					if(maxFreq < floorFreqs[k]){
						maxFreq = floorFreqs[k];
						maxPlace = k+1;
					}
				}
				e.setDestination(maxPlace);
			}
        }
    }
}
